package com.knowledge.plus.io.NIO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author TPF
 * @since 2025/6/9
 */
public class NioClient {
    public static void main(String[] args) throws IOException {
        // 连接服务器
        SocketChannel client = SocketChannel.open();
        client.connect(new InetSocketAddress("localhost", 8080));
        System.out.println("Connected to server");

        // 发送消息
        String message = "Hello from NIO client";
        ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
        client.write(buffer);
        System.out.println("Message sent");

        client.close(); // 关闭连接
    }
}